package com.sbx.controller;

import com.sbx.entity.dto.AppStore;
import com.sbx.service.AppStoreService;
import com.sbx.utils.Result;
import com.sbx.validations.RequiredField;
import com.sbx.validations.NotRequiredField;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/appStore")
@Api(tags = "app商城管理")
public class AppStoreController {
    @Resource
    private AppStoreService appStoreService;

    /**
     * 新增
     */
    @PostMapping
    @ApiOperation("新增商城app")
    public Result saveStoreApp(@RequestBody @Validated(value = RequiredField.class) AppStore appStore) {
        appStoreService.saveStoreApp(appStore);
        return Result.success();
    }

    @ApiOperation("删除商城app")
    @DeleteMapping("{id}")
    public Result deleteAppStoreById(@ApiParam("应用id") @PathVariable String id) {
        appStoreService.deleteAppStoreById(id);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询商城app")
    public Result getAppStorePage(@Validated(value = NotRequiredField.class) AppStore appStore,
                                  @ApiParam("起始页") @RequestParam(defaultValue = "1") Integer pageIndex,
                                  @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {

        return Result.page(appStoreService.getAppStorePage(appStore, pageIndex, pageSize));
    }

    @PutMapping
    @ApiOperation("商城app信息修改")
    public Result updateStoreApp(@RequestBody @Validated(NotRequiredField.class) AppStore appStore) {
        appStoreService.updateStoreApp(appStore);
        return Result.success();
    }


    @GetMapping("/user/{userId}/addApp/{appId}")
    @ApiOperation("新增用户app")
    public Result saveUserApp(@PathVariable @ApiParam("用户id") String userId, @PathVariable @ApiParam("app id") String appId) {
        appStoreService.saveUserApp(userId, appId);
        return Result.success();
    }

    @ApiOperation("删除用户app")
    @DeleteMapping("/user/{userId}/app/{appId}")
    public Result deleteUserAppById(@PathVariable @ApiParam("用户id") String userId, @ApiParam("app id") @PathVariable String appId) {
        appStoreService.deleteUserAppById(userId, appId);
        return Result.success();
    }

    @GetMapping("/user/page")
    @ApiOperation("分页查询用户app")
    public Result getUserAppPage(@ApiParam("用户id") @RequestParam("userId") String userId,
                                 @ApiParam(value = "起始页", defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageIndex,
                                 @ApiParam(value = "每页", defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize) {

        return Result.page(appStoreService.getUserAppPage(userId, pageIndex, pageSize));
    }


}
